package DFS;

import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-20
 * Time:15:29
 * Text：129. 求根节点到叶节点数字之和
 *
 */
public class Text4 {
    List<String> t1=new ArrayList<>();
    public void dfs(TreeNode root,StringBuffer _str){
        StringBuffer str=new StringBuffer(_str);
        str.append(Integer.toString(root.val));
        if(root.left==null && root.right==null){
            t1.add(str.toString());
            return ;
        }
        str.append("->");
        if(root.left!=null){
            dfs(root.left,str);
        }

        if(root.right!=null){
            dfs(root.right,str);
        }
    }
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root,new StringBuffer());
        return t1;
    }

    public static void main(String[] args) {
        Text4 t=new Text4();
        TreeNode t1=new TreeNode(1);
        TreeNode t2=new TreeNode(2);
        TreeNode t3=new TreeNode(3);
        TreeNode t5=new TreeNode(5);
        t1.left=t2;
        t1.right=t3;
        t2.right=t5;
        t.binaryTreePaths(t1);
    }
}
